package com.hyj.ecp.common.dao.impl;

import java.util.*;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ecp.QueryObject;
import com.ecp.ResultObject;
import com.hyj.ecp.common.dao.IjdbcDAO;
@Repository
public class JdbcDao  implements IjdbcDAO {

	@Resource(name="jdbcTemplate")
    public  JdbcTemplate jdbcTemplate;
	
	Logger logger = Logger.getLogger(this.getClass()); 
	
	@Override
	public List<Map<String, Object>> queryForList(String sql,Object[] args){
    	List<Map<String, Object>> list = 
    		this.jdbcTemplate.queryForList(sql, args);
        return list;
    }
	
	@Override
	public <T> T queryForObject(String sql, Object[] args, Class<T> clas) {
		try {
			return this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(clas), args);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	@Override
	public Map<String,Object> queryForMap(String sql, Object[] args) {
		try {
			return this.jdbcTemplate.queryForMap(sql, args);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	@Override
	public List<Map<String, Object>> queryForList(String sql,int pageNo,int pageSize,Object[] args){
		int  offset = (pageNo-1)*pageSize;
		String pageSql = sql + " LIMIT  "+offset+","+pageSize;
		logger.info("分页查询："+pageSql);
    	List<Map<String, Object>> list = 
    		this.jdbcTemplate.queryForList(pageSql, args);
        return list;
    }
	
	public <T> List<T> queryForList(String sql,int pageNo,int pageSize,Object[] args,Class<T> clas){
		int  offset = (pageNo-1)*pageSize;
		String pageSql = sql + " LIMIT  "+offset+","+pageSize;
		logger.info("分页查询总记录数："+pageSql);
    	List<T> list = 
    		this.jdbcTemplate.query(pageSql, args,BeanPropertyRowMapper.newInstance(clas));
    	
        return list;
    }
	
	@Override
	public int queryCount(String sql,Object[] args){
		String countSql = "  SELECT  COUNT(1) FROM ( "+sql+" ) c";
		logger.info("分页查询结果集："+countSql);
		return  this.jdbcTemplate.queryForInt(countSql, args);
	}
	
	@Override
	public int update(String sql,Object[] args){
		return this.jdbcTemplate.update(sql, args);
	}

	@Override
	public  ResultObject pageQuery(String sql,int pageNo,int pageSize, Object[] args) {
		ResultObject vo = new ResultObject();
		if(pageNo>0){
			int rowCount = this.queryCount(sql, args);
			List<Map<String,Object>> rows= this.queryForList(sql,pageNo ,pageSize ,  args); 
	    	vo.setRowCount(rowCount);
	    	vo.setRows(rows);
		}else{
			List<Map<String,Object>> rows= this.queryForList(sql ,  args); 
	    	vo.setRows(rows);
		}
		return vo;
	}

	@Override
	public int queryForInt(String sql, Object[] args) {
	   return jdbcTemplate.queryForInt(sql,args);
	}

	 
}
